{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chia sẻ các bản demo với người khác"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Install the Transformers, Datasets, and Evaluate libraries to run this notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install datasets evaluate transformers[sentencepiece]\n",
    "!pip install gradio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "title = \"Ask Rick a Question\"\n",
    "description = \"\"\"\n",
    "The bot was trained to answer questions based on Rick and Morty dialogues. Ask Rick anything!\n",
    "<img src=\"https://huggingface.co/spaces/course-demos/Rick_and_Morty_QA/resolve/main/rick.png\" width=200px>\n",
    "\"\"\"\n",
    "\n",
    "article = \"Check out [the original Rick and Morty Bot](https://huggingface.co/spaces/kingabzpro/Rick_and_Morty_Bot) that this demo is based off of.\"\n",
    "\n",
    "gr.Interface(\n",
    "    fn=predict,\n",
    "    inputs=\"textbox\",\n",
    "    outputs=\"text\",\n",
    "    title=title,\n",
    "    description=description,\n",
    "    article=article,\n",
    "    examples=[[\"What are you doing?\"], [\"Where should we time travel to?\"]],\n",
    ").launch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gr.Interface(classify_image, \"image\", \"label\").launch(share=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import torch\n",
    "import gradio as gr\n",
    "from torch import nn\n",
    "\n",
    "LABELS = Path(\"class_names.txt\").read_text().splitlines()\n",
    "\n",
    "model = nn.Sequential(\n",
    "    nn.Conv2d(1, 32, 3, padding=\"same\"),\n",
    "    nn.ReLU(),\n",
    "    nn.MaxPool2d(2),\n",
    "    nn.Conv2d(32, 64, 3, padding=\"same\"),\n",
    "    nn.ReLU(),\n",
    "    nn.MaxPool2d(2),\n",
    "    nn.Conv2d(64, 128, 3, padding=\"same\"),\n",
    "    nn.ReLU(),\n",
    "    nn.MaxPool2d(2),\n",
    "    nn.Flatten(),\n",
    "    nn.Linear(1152, 256),\n",
    "    nn.ReLU(),\n",
    "    nn.Linear(256, len(LABELS)),\n",
    ")\n",
    "state_dict = torch.load(\"pytorch_model.bin\", map_location=\"cpu\")\n",
    "model.load_state_dict(state_dict, strict=False)\n",
    "model.eval()\n",
    "\n",
    "\n",
    "def predict(im):\n",
    "    x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.0\n",
    "    with torch.no_grad():\n",
    "        out = model(x)\n",
    "    probabilities = torch.nn.functional.softmax(out[0], dim=0)\n",
    "    values, indices = torch.topk(probabilities, 5)\n",
    "    return {LABELS[i]: v.item() for i, v in zip(indices, values)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "interface = gr.Interface(\n",
    "    predict,\n",
    "    inputs=\"sketchpad\",\n",
    "    outputs=\"label\",\n",
    "    theme=\"huggingface\",\n",
    "    title=\"Sketch Recognition\",\n",
    "    description=\"Who wants to play Pictionary? Draw a common object like a shovel or a laptop, and the algorithm will guess in real time!\",\n",
    "    article=\"<p style='text-align: center'>Sketch Recognition | Demo Model</p>\",\n",
    "    live=True,\n",
    ")\n",
    "interface.launch(share=True)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "Chia sẻ các bản demo với người khác",
   "provenance": []
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
